﻿#include "register.h"
#include "ui_register.h"
Register::Register(ChatSocket *c,QWidget *parent) :
    QWidget(parent),cs(c),
    ui(new Ui::Register)
{
    ui->setupUi(this);
    connect(cs,SIGNAL(registerRpt(QJsonObject)),this,SLOT(slotRegisterRpt(QJsonObject)));
}

Register::~Register()
{
    delete ui;
}

void Register::on_pushButton_getvfr_clicked()
{
    //获取验证码
    QString mail = ui->email->text();
    if ("" == mail)
    {
        QMessageBox::warning(this,"error","请填写正确邮箱地址");
        return;
    }
    cs->getVerifyCode(mail);
    QMessageBox::warning(this,"verify_Code","验证码将发送至您的邮箱，请注意查收");
}

void Register::on_pushButton_regi_clicked()
{
    //注册
    if ("" == ui->user_name->text() || "" == ui->pwd->text() || "" == ui->real_name->text() \
         || "" == ui->email->text() || "" == ui->verify->text())
    {
        //有参数未填
        QMessageBox::warning(this,"error","请完整填写信息");
        return;
    }
    //发送注册请求
    QString icon = "";
    cs->registerRequest(icon,ui->user_name->text(),ui->pwd->text(),ui->real_name->text(),ui->address->text(),\
                        ui->email->text(),ui->verify->text());
}

void Register::slotRegisterRpt(QJsonObject root)
{
    //注册结果返回
    qDebug()<<"response";
    int id = root[STR_ID].toInt();
    if (id <= 0)
    {
        //失败
        QMessageBox::warning(this,"waring",root[STR_MSG].toString());
    } else {
        //成功
        QString text = QString("注册成功\nid:%1\nid同时会发至邮箱").arg(id);
        QMessageBox::warning(this,"success",text);
        this->close();
    }
}
